package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginDTO;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveDTO;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/4 11:27
 * @Service注解表示当前类为业务层实现类
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录业务
     * @param userLoginDTO
     * @return UserVO
     */
    @Override
    public UserVO login(UserLoginDTO userLoginDTO) {
        log.debug("登录业务:" + userLoginDTO);
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        } else {
            if (!userVO.getPassword().equals(userLoginDTO.getPassword())) {
                throw new ServiceException(StatusCode.PASSWORD_ERROR);
            }
        }
        return userVO;
    }

    /**
     * 新建用户业务
     * @param userSaveDTO
     * @return int
     */
    @Override
    public int save(UserSaveDTO userSaveDTO) {
        log.debug("保存用户业务:" + userSaveDTO);
        User user = new User();
        BeanUtils.copyProperties(userSaveDTO, user);
        // 如果用户id为空,则表示新增
        if (user.getId() == null) {
            log.debug("新增用户业务:" + user);
            UserVO userVO = userMapper.selectByUsername(userSaveDTO.getUsername());
            // 如果用户名存在,则抛出异常
            if (userVO != null) {
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            // 设置默认密码,员工的账号不是自己注册的,所以需要系统设置初始密码
            user.setPassword("123456");
            user.setCreateTime(new Date());
            int result = userMapper.insert(user);
            if (result != 1) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        } else {
            log.debug("修改用户业务:" + user);
            // 修改用户数据
            user.setUpdateTime(new Date());
            return userMapper.update(user);
        }
    }

    /**
     * 重置密码业务
     * @param userId
     * @return int
     */
    @Override
    public int resetPassword(Long userId) {
        User user = new User();
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        int result = userMapper.update(user);
        if (result == 0) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 查询全部用户业务
     * @param userQuery
     * @return List<UserVO>
     */
    @Override
    public List<User> selectUser(UserQuery userQuery) {
        return userMapper.selectUser(userQuery);
    }

    /**
     * 修改用户状态业务
     * @param userId
     * @param status
     * @return int
     */
    @Override
    public int updateStatus(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        int result = userMapper.update(user);
        if (result == 0) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 删除用户业务
     * @param userId
     * @return int
     */
    @Override
    public int delete(Long userId) {
        int result = userMapper.delete(userId);
        if (result == 0) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 查询待审批用户业务
     * @param parentId
     * @return List<User>
     */
    @Override
    public List<User> selectAudit(Long parentId) {
        ArrayList<User> userList =  new ArrayList<>();
        User user1 = userMapper.selectAuditById(parentId);
        userList.add(user1);
        if (user1 != null && user1.getParentId() != null){
            User user2 = userMapper.selectAuditById(user1.getParentId());
            userList.add(user2);
        }
        return userList;
    }
}
